package com.ddpt.permission.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lh.center.common.base.BizServiceImpl;
import com.lh.center.common.base.ResponseEntity;
import com.lh.center.common.constant.GlobalConstant;
import com.lh.center.common.exception.CommonException;
import com.lh.center.common.exception.ErrorEnum;
import com.lh.center.common.util.AssertUtil;
import com.lh.center.permission.api.entity.MerchantAdminUser;
import com.lh.center.permission.api.request.MerchantEmpCountQueryRequest;
import com.lh.center.permission.api.request.UpdateMerchantUserPasswordReq;
import com.lh.center.permission.api.response.MerchantEmpCountResponse;
import com.lh.center.permission.mapper.MerchantAdminUserMapper;
import com.lh.center.permission.service.MerchantAdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商户管理员 服务实现类
 * </p>
 *
 * @author czy
 * @since 2022-04-07
 */
@Service
public class MerchantAdminUserServiceImpl extends BizServiceImpl<MerchantAdminUserMapper, MerchantAdminUser> implements MerchantAdminUserService {

    @Autowired
    private MerchantAdminUserMapper merchantAdminUserMapper;

    @Override
    public void createMerchantAdmin(MerchantAdminUser adminUser) {
        checkAdminUsernameUnique(adminUser.getId(),adminUser.getUsername());
        adminUser.setType(GlobalConstant.SUPER_ADMIN);
        adminUser.setDataAuth(2);
        adminUser.setRealName("超级管理员");
        save(adminUser);
    }

    @Override
    public void checkAdminUsernameUnique(Long id, String username) {
        MerchantAdminUser adminUser = getOne(Wrappers.lambdaQuery(MerchantAdminUser.class).eq(MerchantAdminUser::getUsername, username));
        if(null != adminUser && !adminUser.getId().equals(id)) {
            throw new CommonException(ErrorEnum.ADMIN_USERNAME_DUPLICATE);
        }
    }

    @Override
    public ResponseEntity<List<MerchantEmpCountResponse>> queryMerchantEmpCount(MerchantEmpCountQueryRequest queryRequest) {
        List<MerchantEmpCountResponse> responseList = merchantAdminUserMapper.queryMerchantEmpCount(queryRequest);
        return ResponseEntity.success(responseList);
    }

    @Override
    public void resetPasswordFromBack(MerchantAdminUser adminUser) {
        LambdaQueryWrapper<MerchantAdminUser> wrapper = Wrappers.lambdaQuery(MerchantAdminUser.class);
        wrapper.eq(MerchantAdminUser::getMerchantId,adminUser.getMerchantId());
        wrapper.eq(MerchantAdminUser::getUsername,adminUser.getUsername());
        MerchantAdminUser oldAdmin = getOne(wrapper);
        if(null == oldAdmin) {
            throw new CommonException(ErrorEnum.PARAM_ERROR);
        }
        MerchantAdminUser update = new MerchantAdminUser();
        update.setId(oldAdmin.getId());
        update.setPassword(DigestUtil.md5Hex(adminUser.getPassword()));
        updateById(update);
    }

    /**
     * 根据用户名和密码查询商户管理员
     * @param username
     * @param password 明文密码
     * @return
     */
    @Override
    public MerchantAdminUser queryByUsernameAndPassword(String username, String password) {
        LambdaQueryWrapper<MerchantAdminUser> wrapper = Wrappers.lambdaQuery(MerchantAdminUser.class);
        wrapper.eq(MerchantAdminUser::getUsername,username);
        wrapper.eq(MerchantAdminUser::getPassword,DigestUtil.md5Hex(password));
        wrapper.eq(MerchantAdminUser::getDeleted,GlobalConstant.ZERO);
        return get(wrapper);
    }

    @Override
    public MerchantAdminUser queryByUsernameAndPasswordCallBack(String username, String password) {
        LambdaQueryWrapper<MerchantAdminUser> wrapper = Wrappers.lambdaQuery(MerchantAdminUser.class);
        wrapper.eq(MerchantAdminUser::getUsername,username);
        wrapper.eq(MerchantAdminUser::getPassword,password);
        wrapper.eq(MerchantAdminUser::getDeleted,GlobalConstant.ZERO);
        return getOne(wrapper);
    }

    @Override
    public List<Long> queryIdByIns(List<Long> dataInsIds) {
        List<MerchantAdminUser> adminUsers = list(Wrappers.lambdaQuery(MerchantAdminUser.class).in(MerchantAdminUser::getInsId,dataInsIds).eq(MerchantAdminUser::getDeleted,GlobalConstant.ZERO));
        return adminUsers.stream().map(MerchantAdminUser::getId).collect(Collectors.toList());
    }

    @Override
    public MerchantAdminUser getMerchanAdminUser(Long id) {
        return merchantAdminUserMapper.selectById(id);
    }

    @Override
    public void bindWx(Long id, String openId) {
        MerchantAdminUser user = merchantAdminUserMapper.selectById(id);
        user.setBindWechat(1);
        user.setWxOpenId(openId);
        merchantAdminUserMapper.updateById(user);
    }

    @Override
    public void unBindWx(Long id) {
        LambdaUpdateWrapper<MerchantAdminUser> wrapper = Wrappers.lambdaUpdate(MerchantAdminUser.class).eq(MerchantAdminUser::getId, id).set(MerchantAdminUser::getWxOpenId, null).set(MerchantAdminUser::getBindWechat, 0);
        update(wrapper);
    }

    @Override
    public void updatePassword(UpdateMerchantUserPasswordReq updateMerchantUserPasswordDot) {
        AssertUtil.isEmpty(updateMerchantUserPasswordDot.getOldPassword(), "旧密码不能为空");
        AssertUtil.isEmpty(updateMerchantUserPasswordDot.getNewPassword(), "新密码不能为空");
        MerchantAdminUser merchantAdminUser = merchantAdminUserMapper.selectById(updateMerchantUserPasswordDot.getId());
        AssertUtil.isTrue(!merchantAdminUser.getPassword().equals(DigestUtil.md5Hex(updateMerchantUserPasswordDot.getOldPassword())), "旧密码错误");
        merchantAdminUser.setPassword(DigestUtil.md5Hex(updateMerchantUserPasswordDot.getNewPassword()));
        updateById(merchantAdminUser);
    }
}
